package model
{
	import utils.EnterFrameTimer;

	public class TimerModel
	{
		private static var instance:TimerModel;
		
		private var timers:Array;
		
		public function TimerModel()
		{
			if (instance)
			{
				throw Error("单列");
			}
			
		}
		
		public static function getInstance():TimerModel
		{
			if (instance == null)
			{
				instance = new TimerModel();
				instance.init();
			}
			return instance;
		}
		
		private function init():void
		{
			timers = [];
		}
		
		public function step():void
		{
			for (var i:String in timers)
			{
				var enterFrameTimer:EnterFrameTimer = timers[i];
				enterFrameTimer.step();
			}
			
		}
		
		public function addTimer(timer:EnterFrameTimer):void
		{
			if (timer)
			{
				if (timers.indexOf(timer) < 0)
				{
					timers.push(timer);				
				}
			}
		}
		
		public function removeTimer(timer:EnterFrameTimer):void
		{
			if (timer)
			{
				var index:int = timers.indexOf(timer);
				if (index >= 0)
				{
					timers.splice(index,1);
				}
			}
		}
		
		public function clear():void
		{
			timers.length = 0;
		}
	}
}